using System;
using System.Collections.Generic;
using System.Text;
using csharp.rad.general;

namespace csharp.rad.sisponto
{
   public class Log
   {
      private Database bd;
      public Database.Result result = new Database.Result();
      private String msgErro;
      private Int32 l_id_log;
      private DateTime l_data;
      private TimeSpan l_hora;
      private String l_id_user;

      public Log(Database bancoDeDados)
      {
         bd = bancoDeDados;
      }
      public Int32 id_log
      {
         get { return l_id_log; }
         set { l_id_log = value; }
      }
      public DateTime data
      {
         get { return l_data; }
         set { l_data = value; }
      }
      public TimeSpan hora
      {
         get { return l_hora; }
         set { l_hora = value; }
      }
      public String id_user
      {
         get { return l_id_user; }
         set { l_id_user = value; }
      }

      public int inserir()
      {
         String _sql;

         _sql = "INSERT INTO log (data,hora,id_user) "
               + "VALUES('" + l_data.ToString("yyyy/MM/dd") + "','" + l_hora + "','" + l_id_user + "')";


         result = bd.ExecuteCommand(_sql, Database.doNotUseTransaction);

         if (result.Status == Database.Status.Failed)
         {
            msgErro = result.ErrorMessage;
            return -1;
         }
         return 1;
      }

      public System.Data.DataSet obter()
      {
         string _sql;
         System.Data.DataSet dtsResult;

         _sql = "SELECT data, hora, user_name "
              + "FROM log l JOIN users u "
                    + " ON l.id_user = u.user_id "
              + "ORDER BY data, hora, id_user";

         dtsResult = bd.ExecuteQuery(_sql, "log", out result);
         return dtsResult;
      }
   }
}
